package com.hz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hz.dao.UserDao;
import com.hz.pojo.User;
import com.hz.service.UserService;
import com.hz.utils.MassageJsons;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Override
    public User getUserInfo(String open_id) {
        return userDao.getUserInfo(open_id);
    }

    @Override
    @Transactional
    public int addUserInfo(User user) {
        userDao.addUserDfuSafeInfo(user);
        return userDao.addUserInfo(user);
    }


    @Override
    public MassageJsons addUserPhoneInfo(String open_id, String user_phone) {
        //修改手机号
        int i =userDao.addUserPhoneInfo(open_id,user_phone);
        //修改支付宝
        userDao.addUserAlipayInfo(open_id,user_phone);
        if(i!=0){
            //更新Radis数据
            String userjson = JSONObject.toJSONString(userDao.getUserInfo(open_id));
            stringRedisTemplate.opsForValue().set(open_id,userjson);
            return new MassageJsons("0","绑定成功");
        }
        return new MassageJsons("1","绑定失败");
    }

    @Override
    //修改支付宝
    public int addUserAlipayInfo(String open_id, String user_phone) {
        return userDao.addUserAlipayInfo(open_id,user_phone);
    }

    @Override
    public MassageJsons updLlastLoginTime(String open_id, String user_last_login_time) throws Exception {
        userDao.updLlastLoginTime(open_id,user_last_login_time);
        return new MassageJsons("0","修改成功");
    }

    @Override
    public User getUserById(Long user_id) throws Exception {
        return userDao.getUserById(user_id);
    }

    @Override
    public int updUserName(String user_name, String open_id) throws Exception {

        return userDao.updUserName(user_name,open_id);
    }

    @Override
    public List<User> getDirect(Long user_id) throws Exception {
        List<User> userList = null;
        userList = userDao.getDirect(user_id);

        return userList;
    }


}
